package com.enjoyor.soa.traffic.core.efoms.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.enjoyor.soa.traffic.core.efoms.domain.CheckRecodeInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.DeviceFaultCollectInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.DeviceNowCollectSum;
import com.enjoyor.soa.traffic.core.efoms.domain.DeviceUnnormalCollectInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.OppmWorkCollectInfo;
import com.enjoyor.soa.traffic.core.efoms.domain.RepairsInfo;
import com.enjoyor.soa.traffic.core.efoms.mapper.ICheckRecodeInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.mapper.IDeviceFaultCollectInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.mapper.IDeviceNowCollectSumMapper;
import com.enjoyor.soa.traffic.core.efoms.mapper.IDeviceUnnormalCollectInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.mapper.IOppmWorkCollectInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.mapper.IRepairsInfoMapper;
import com.enjoyor.soa.traffic.core.efoms.service.ISurveyDeviceCollectInfoService;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;

@Service
@Transactional
@SuppressWarnings("rawtypes")
public class SurveyDeviceCollectInfoService implements ISurveyDeviceCollectInfoService{

	@Autowired
	private IDeviceUnnormalCollectInfoMapper deviceUnnormalCollectInfoMapper;
	
	@Autowired
	private IDeviceNowCollectSumMapper deviceNowCollectSumMapper;
	
	@Autowired
	private ICheckRecodeInfoMapper checkRecodeInfoMapper;
	
	@Autowired
	private IRepairsInfoMapper repairsInfoMapper;
	
	@Autowired
	private IDeviceFaultCollectInfoMapper deviceFaultCollectInfoMapper;
	
	@Autowired
	private IOppmWorkCollectInfoMapper oppmWorkCollectInfoMapper;
	
	@Override
	public Map<String, Object> getDeviceUnnormalCollectInfoList(String devTypeCode) {
		// TODO Auto-generated method stub
		Map<String, Object> pramT = this.getSelectMap(devTypeCode);
		try {
			List<DeviceUnnormalCollectInfo> list = deviceUnnormalCollectInfoMapper.getDeviceUnnormalCollectInfoList(pramT);
			int sum = 0;
			int unNoramlSum = 0;
			for(int i = 0;i<list.size();i++){
				String persent = list.get(i).getHealthRate()+"%";
				list.get(i).setHealthRate(persent);
				int num = Integer.parseInt(list.get(i).getDevSum());
				int unnum = Integer.parseInt(list.get(i).getTotalSum());
				sum = sum + num;
				unNoramlSum = unNoramlSum+unnum;
			}
			Map<String, Object> resultMap = new HashMap<>();
			resultMap.put("devSum", sum);
			resultMap.put("unNoramlSum", unNoramlSum);
			resultMap.put("list", list);
			return resultMap;
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}
	
	@Override
	public List<DeviceUnnormalCollectInfo> getDeviceUnNorForArea(String devTypeCode) {
		// TODO Auto-generated method stub
		Map<String, Object> pramT = this.getSelectMap(devTypeCode);
		try {
			List<DeviceUnnormalCollectInfo> ListSum = deviceUnnormalCollectInfoMapper.getDeviceSumForArea(pramT);
			List<DeviceUnnormalCollectInfo> listNoNormal = deviceUnnormalCollectInfoMapper.getDeviceUnNorForArea(pramT);
			for(int i = 0;i<ListSum.size();i++){
				for(int j = 0;j<listNoNormal.size();j++){
					if(ListSum.get(i).getDevAreaCode().equals(listNoNormal.get(j).getDevAreaCode())){
						int norNum = Integer.parseInt(ListSum.get(i).getDevSum())-Integer.parseInt(listNoNormal.get(j).getTotalSum());
						ListSum.get(i).setNormanlNum(String.valueOf(norNum));
						ListSum.get(i).setTotalSum(listNoNormal.get(j).getTotalSum());
						
					}else{
						//ListSum.get(i).setNormanlNum(ListSum.get(i).getDevSum());
					}
				}
			}
			return ListSum;
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
		
	}
	
	@Override
	public List<DeviceNowCollectSum> getDeviceNowCollectSum(String beginTime, String endTime,String devTypeCode) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> pramT = this.getDateMap(beginTime, endTime,devTypeCode);
			return deviceNowCollectSumMapper.getDeviceNowCollectSum(pramT);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}
	
	@Override
	public Map<String, Object> getCheckCountForDeptIdAndSum() {
		// TODO Auto-generated method stub
		try {
			List<CheckRecodeInfo> list = checkRecodeInfoMapper.getCheckCountForDeptIdAndSum();
			int sum = 0;
			for(int i = 0;i<list.size();i++){
				int num = Integer.parseInt(list.get(i).getCount());
				sum = sum + num;
			}
			Map<String, Object> resultMap = new HashMap<>();
			resultMap.put("devSum", sum);
			resultMap.put("list", list);
			return resultMap;
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}
	
	@Override
	public Map<String, Object> getRepCountForDeptIdAndSum() {
		// TODO Auto-generated method stub
		try {
			List<RepairsInfo> list = repairsInfoMapper.getRepCountForDeptIdAndSum();
			int sum = 0;
			for(int i = 0;i<list.size();i++){
				int num = Integer.parseInt(list.get(i).getCount());
				sum = sum + num;
			}
			Map<String, Object> resultMap = new HashMap<>();
			resultMap.put("devSum", sum);
			resultMap.put("list", list);
			return resultMap;
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}
	
	@Override
	public List<RepairsInfo> getRepSourceCount(String devTypeCode) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> pramT = this.getSelectMap(devTypeCode);
			return repairsInfoMapper.getRepSourceCount(pramT);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public List<RepairsInfo> getRepSourceCountForDept(String devTypeCode) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> pramT = this.getSelectMap(devTypeCode);
			return repairsInfoMapper.getRepSourceCountForDept(pramT);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
		
	}
	
	@Override
	public List<DeviceUnnormalCollectInfo> getFaultCollectReal(String devTypeCode) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> pramT = this.getSelectMap(devTypeCode);
			return deviceUnnormalCollectInfoMapper.getFaultCollectReal(pramT);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}
	
	@Override
	public List<DeviceUnnormalCollectInfo> getFaultCollectRealTrend(String devTypeCode, String beginTime,
			String endTime) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> pramT = this.getTrendMap(devTypeCode, beginTime, endTime);
			return deviceUnnormalCollectInfoMapper.getFaultCollectRealTrend(pramT);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}
	
	@Override
	public Map<String, Object> selectDeviceFaultCollectInfoList(String devTypeCode) {
		// TODO Auto-generated method stub
		Map<String, Object> pramT = this.getSelectMap(devTypeCode);
		try {
			List<DeviceFaultCollectInfo> list = deviceFaultCollectInfoMapper.selectDeviceFaultCollectInfoList(pramT);
			int sum = 0;
			for(int i = 0;i<list.size();i++){
				int num = Integer.parseInt(list.get(i).getCount());
				sum = sum + num;
			}
			Map<String, Object> resultMap = new HashMap<>();
			resultMap.put("devSum", sum);
			resultMap.put("list", list);
			return resultMap;
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}

	@Override
	public List<DeviceFaultCollectInfo> selectDeviceFaultCollectByDevTypeCode(String devTypeCode) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> pramT = this.getSelectMap(devTypeCode);
			return deviceFaultCollectInfoMapper.selectDeviceFaultCollectByDevTypeCode(pramT);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}
	
	@Override
	public List<DeviceFaultCollectInfo> selectDevFaColGroupByDepId(String devTypeCode) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> pramT = this.getSelectMap(devTypeCode);
			return deviceFaultCollectInfoMapper.selectDevFaColGroupByDepId(pramT);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}
	
	@Override
	public List<OppmWorkCollectInfo> selectOppmWorkCollectInfoList(String devTypeCode) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> map = new HashMap<>();
			Date beginTime = new Date();
			String endTime = TimeHelper.convertWeekByDate(beginTime);
			map.put("devTypeCode", devTypeCode);
			map.put("endTime", endTime);
			return oppmWorkCollectInfoMapper.selectOppmWorkCollectInfoList(map);
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}
	
	@Override
	public Map<String, Object> getWarningCountForToday(String devTypeCode) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> map = this.getSelectMap(devTypeCode);
			//int warnHis = deviceUnnormalCollectInfoMapper.getWarnHisCount(map);
			int warnReal = deviceUnnormalCollectInfoMapper.getWarnRealCount(map);
			//int totalSum = warnHis+warnReal;
			Map<String, Object> resultMap = new HashMap<>();
			resultMap.put("totalSum", warnReal);
			return resultMap;
		} catch (Exception e) {
			// TODO: handle exception
			LoggerHelper.LOG.error(e);
			throw e;
		}
	}
	
	//----------------------------------私有方法开始--------------------------------------------//
	/**
	 * 获取查询的map集合；
	 * @param devTypeCode
	 * @return
	 */
	private Map<String, Object> getSelectMap(String devTypeCode){
		Map<String, Object> map = new HashMap<>();
		map.put("devTypeCode", devTypeCode);
		return map;
	}
	
	private Map<String, Object> getDateMap(String beginTime, String endTime,String devTypeCode){
		Map<String, Object> map = new HashMap<>();
		map.put("beginTime", beginTime);
		map.put("endTime", endTime);
		map.put("devTypeCode", devTypeCode);
		return map;
	}

	private Map<String, Object> getTrendMap(String devTypeCode,String beginTime, String endTime){
		Map<String, Object> map = new HashMap<>();
		map.put("beginTime", beginTime);
		map.put("endTime", endTime);
		map.put("devTypeCode", devTypeCode);
		return map;
	}

}
